package com.example.foodiemoments.ui.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.foodiemoments.R
import com.example.foodiemoments.databinding.FragmentHomeBinding
import com.example.foodiemoments.ui.adapters.RecipeAdapter
import com.example.foodiemoments.ui.models.Recipe

class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null
    private val binding get() = _binding!!

    private lateinit var recipeAdapter: RecipeAdapter

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupViews()
        loadMockData()
    }

    private fun setupViews() {
        // 搜索框点击
        binding.searchBar.setOnClickListener {
            // TODO: 跳转到搜索页面
        }

        // 分类按钮
        binding.btnCategory.setOnClickListener {
            // TODO: 显示分类对话框
        }

        // RecyclerView
        recipeAdapter = RecipeAdapter()
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = recipeAdapter
        }

        // 下拉刷新
        binding.swipeRefreshLayout.setOnRefreshListener {
            loadMockData()
            binding.swipeRefreshLayout.isRefreshing = false
        }
    }

    private fun loadMockData() {
        val mockRecipes = listOf(
            Recipe(
                id = 1,
                title = "红烧肉 | 肥而不腻，入口即化",
                author = "美食达人小王",
                authorAvatar = R.drawable.ic_profile,
                coverImage = R.drawable.ic_foodie_logo,
                likeCount = 1520,
                commentCount = 238,
                collectCount = 892,
                description = "传统红烧肉的做法，五花肉选用三层肉最佳..."
            ),
            Recipe(
                id = 2,
                title = "糖醋排骨 | 酸甜开胃，老少皆宜",
                author = "厨房新手小李",
                authorAvatar = R.drawable.ic_profile,
                coverImage = R.drawable.ic_foodie_logo,
                likeCount = 980,
                commentCount = 156,
                collectCount = 567,
                description = "简单易学的糖醋排骨，色泽红亮，口感酥嫩..."
            ),
            Recipe(
                id = 3,
                title = "麻婆豆腐 | 川菜经典，麻辣鲜香",
                author = "川菜小师傅",
                authorAvatar = R.drawable.ic_profile,
                coverImage = R.drawable.ic_foodie_logo,
                likeCount = 2100,
                commentCount = 445,
                collectCount = 1234,
                description = "正宗川味麻婆豆腐，豆瓣酱是灵魂..."
            )
        )

        recipeAdapter.submitList(mockRecipes)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
